﻿using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MySparrow
{
    public enum ShrinkDirection
    {
        Horizontal = 0,
        Vertical = 1
    };
    public class ShrinkArea
    {
        int shrinkAreaHeight = 0;
        int shrinkAreaWidth = 0;
        bool expanded = true;
        Form form;
        Hashtable shrinkAreaControls = new Hashtable();
        Hashtable belowShrinkAreaControls = new Hashtable();

        public ShrinkArea(Form form, int shrinkAreaTop, int shrinkAreaBottom,ShrinkDirection sd)
        {

            this.form = form;

            switch (sd)
            {
                case ShrinkDirection.Horizontal:
                    this.shrinkAreaWidth = shrinkAreaBottom - shrinkAreaTop;
                    foreach (Control control in form.Controls)
                    {
                        if (shrinkAreaTop <= control.Location.X &&
                            control.Location.X < shrinkAreaBottom)
                        {
                            shrinkAreaControls.Add(control, null);
                        }

                        if (control.Location.X >= shrinkAreaBottom)
                        {
                            belowShrinkAreaControls.Add(control, null);
                        }
                    }
                    break;
                case ShrinkDirection.Vertical:
                    this.shrinkAreaHeight = shrinkAreaBottom - shrinkAreaTop;
                    foreach (Control control in form.Controls)
                    {
                        if (shrinkAreaTop <= control.Location.Y &&
                            control.Location.Y < shrinkAreaBottom)
                        {
                            shrinkAreaControls.Add(control, null);
                        }

                        if (control.Location.Y >= shrinkAreaBottom)
                        {
                            belowShrinkAreaControls.Add(control, null);
                        }
                    }
                    break;
            }
        }

        public void Toggle()
        {

            int diffHeight = 0;
            int diffWidth = 0;

            diffWidth = expanded ? -shrinkAreaWidth : shrinkAreaWidth;

            diffHeight = expanded ? -shrinkAreaHeight : shrinkAreaHeight;

            form.SuspendLayout();


            foreach (Control control in form.Controls)
            {
                if (shrinkAreaControls.Contains(control))
                {
                    control.Visible = !expanded;
                }

                if (belowShrinkAreaControls.Contains(control))
                {
                    control.Left += diffWidth;
                    control.Top += diffHeight;
                }
            }

            form.Size = new Size(form.Size.Width + diffWidth,
                                 form.Size.Height + diffHeight);

            form.ResumeLayout();

            expanded = !expanded;
        }

        public bool Expanded
        {
            get
            {
                return expanded;
            }
            set
            {
                if (expanded == value)
                {
                    return;
                }

                Toggle();
            }
        }
    }
}
